﻿using Common;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Messaging;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MsgRec
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var path = @".\private$\" + this.textBox1.Text;
            var mq = CreateMessageQueue(path);
            if(mq != null)
            {
                mq.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) });
                IntPtr v = this.Handle;
                var bus = new MyBus(mq,v); 

                Thread thread = new Thread(bus.Receiver);
                thread.Start();
            }
            
        }

        private void recMsg()
        {
           
        }

        protected override void DefWndProc(ref System.Windows.Forms.Message m)
        {
            const int BM_CLICK = 0xF5;

            const int BM_CLICK2 = 0xF6;

            if (m.Msg == BM_CLICK)
            {
                int i = 100;
                MessageBox.Show("子窗体收到事件");
            }
            else if(m.Msg == BM_CLICK2)
            {
                COPYDATASTRUCT cdata = new COPYDATASTRUCT();
                Type mytype = cdata.GetType();
                cdata = (COPYDATASTRUCT)m.GetLParam(mytype);
                var a = cdata.dwData.ToString();
                var b = cdata.lpData; 
                this.textBox2.Text =string.Format("{0} mesRecData:" + b + "\r\n" + this.textBox2.Text,DateTime.Now.ToString("MM月dd日 HH:mm:ss"));
                
            }
            else
            {
                base.DefWndProc(ref m);
            } 
        }

        protected override void  WndProc(ref System.Windows.Forms.Message m)
        {
            const int BM_CLICK = 0xF5;

            if (m.Msg == BM_CLICK)
            {
                MessageBox.Show("子窗体收到事件");
            }
            else
            {
                base.WndProc(ref m);
            }
        }
        public static MessageQueue CreateMessageQueue(string path)
        {
            MessageQueue mq = null;

            if (MessageQueue.Exists(path))
            {
                mq = new MessageQueue(path);
            }
            else
            {
                mq = MessageQueue.Create(path, true);
            }

            return mq;
        }
    }
    public class MyBus :Bus
    {
        IntPtr v;
        public MyBus(MessageQueue msmq, IntPtr v) :base(msmq,"")
        {
            this.v = v;
        }

        const int BM_CLICK = 0xF5;
        [DllImport("User32.dll", EntryPoint = "SendMessage")]
        private static extern int SendMessage(int hwnd, int wMsg, int wParam, int lParam);

        [DllImport("User32.dll", EntryPoint = "FindWindow")]
        private static extern int FindWindow(string lpClassName, string lpWindowName);


      
        /// <summary>
        /// 定义发送消息方法SendMessage
        /// </summary>
        /// <param name="hWnd">要发送消息的窗口句柄</param>
        /// <param name="msg">消息标识符消息ID</param>
        /// <param name="wParam">16位整型变量</param>
        /// <param name="cdata">自定义消息</param>
        /// <returns></returns>
        [DllImport("user32.dll", EntryPoint = "SendMessage")]
        private static extern int SendMessageStr(IntPtr hWnd, int Msg, int wParam, ref COPYDATASTRUCT cdata);

        private void SendMessage(string pcMessage, string pcReceiveForm)
        {
            int WINDOW_HANDLER = FindWindow(null, pcReceiveForm);
            
                SendMessage(v.ToInt32(), BM_CLICK, 0, 0);
            
        }

        const int BM_CLICK2 = 0xF6;
        private void SendMessage2(COPYDATASTRUCT cdata)
        {
            SendMessageStr(this.v, BM_CLICK2, 0, ref cdata);
        }
         
        public override void Receiver()
        {
            while (true)
            {
                try
                {
                    System.Messaging.Message m = _msmq.Receive(MessageQueueTransactionType.Single); 
                    var val = m.Body.ToString();

                    COPYDATASTRUCT a = new COPYDATASTRUCT();
                    a.cData = 100 ;
                    a.dwData = v;
                    a.lpData = val;
                    this.SendMessage2(a);
                    System.Threading.Thread.Sleep(1000);
                    //this.SendMessage(val,val); 
                }
                catch (Exception ex)
                {

                }
            }
        }
    }
    public struct COPYDATASTRUCT
    {
        public IntPtr dwData;
        public int cData;
        [MarshalAs(UnmanagedType.LPStr)]
        public string lpData;
    }
}
